//
//  YDWelcomeController.swift
//  weibo
//
//  Created by 孙亚东 on 16/6/26.
//  Copyright © 2016年 Sunyadong. All rights reserved.
//

import UIKit



class YDWelcomeController: UIViewController {
    
    
    
    override func loadView() {
        
        view = bjImageView
        
        
        
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
        
        
    }

    
    // 视图出现之后,再动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    
    func setupUI() {
    
    
        bjImageView.addSubview(photoView)
        bjImageView.addSubview(welcomeLab)
    
    
        //设置frame
        photoView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(bjImageView.snp_centerX)
            
            make.top.equalTo(bjImageView.snp_bottom).offset(-100)
            
            
        }
        
        
        welcomeLab.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(photoView)
            make.top.equalTo(photoView.snp_bottom).offset(10)
            
            
        }
        
 
    }
    
    //设置动画
    
    func startAnimation () {
    
        /**
        <#Description#>
        
        - parameter delay:                  <#delay description#>
        - parameter usingSpringWithDamping: <#usingSpringWithDamping description#>
        - parameter initialSpringVelocity:  <#initialSpringVelocity description#>
        - parameter options:                <#options description#>
        - parameter animations:             <#animations description#>
        - parameter completion:             <#completion description#>
        */
        
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
            //要完成的动画,改变frame
            
            self.photoView.snp_updateConstraints(closure: { (make) -> Void in
                
                make.top.equalTo(self.bjImageView.snp_bottom).offset(-screenHeight + 100)
                
        
            })
            
            //通过设置约束，开始动画，必须实现下面的防范
 
            self.view.layoutIfNeeded()

            
            }) { (Bool) -> Void in
                
            //动画完成之后要干的事情，欢迎界面显示后，到达主页界面
 /// mark ---- 利用通知返回到主页
                
            NSNotificationCenter.defaultCenter().postNotificationName("welcome", object: self)
                
                
                
                
        }
        

    
    
    
    
    
    }
    
    
    
    
    
    
    
    // 懒加载背景vieW
    
    lazy var bjImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    
    // 懒加载图片weiview
    
    lazy var photoView: UIImageView = {
    
        let photoView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
        photoView.layer.cornerRadius = 42.5

        photoView.layer.masksToBounds = true
        
        
        photoView.sizeToFit()

        return photoView
    }()
    
    
    //懒加载lab
    
    lazy var welcomeLab: UILabel = {
    
    
        let welcomeLab = UILabel()
        
        welcomeLab.text = "欢迎回来"
        
    
        return welcomeLab
    
    }()
    
    

}
